﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace My
{
    /// <summary>
    /// MyCalendar 的摘要描述
    /// </summary>
    public class MyCalendar : Calendar
    {
        public MyCalendar()
        {
            //
            // TODO: 在此加入建構函式的程式碼
            //
        }

        protected override void OnSelectionChanged()
        {
            foreach(DateTime d in SelectedDates)
            {
                if (MySelectedDates.Contains(d))
                {
                    CurrentDeleteDates.Add(d);
                    MySelectedDates.Remove(d);
                }
                else
                {
                    CurrentAddDates.Add(d);
                    MySelectedDates.Add(d);
                }
            }
           
            //reconstruct selected dates
            SelectedDates.Clear();

            foreach (DateTime d in MySelectedDates)
            {
                SelectedDates.Add(d);
            }

            base.OnSelectionChanged();
        }

        protected override void OnDayRender(TableCell cell, CalendarDay day)
        {
            base.OnDayRender(cell, day);
        }

        public List<DateTime> MySelectedDates
        {
            get
            {
                if (ViewState["MySelectedDates"] == null)
                {
                    ViewState["MySelectedDates"] = new List<DateTime>();
                }

                return ViewState["MySelectedDates"] as List<DateTime>;
            }
        }

        public List<DateTime> CurrentAddDates
        {
            get
            {
                if (Context.Items[this.ID + "CurrentAddDates"] == null)
                {
                    Context.Items[this.ID + "CurrentAddDates"] = new List<DateTime>();
                }

                return Context.Items[this.ID + "CurrentAddDates"] as List<DateTime>;
            }
        }

        public List<DateTime> CurrentDeleteDates
        {
            get
            {
                if (Context.Items[this.ID + "CurrentDeleteDates"] == null)
                {
                    Context.Items[this.ID + "CurrentDeleteDates"] = new List<DateTime>();
                }

                return Context.Items[this.ID + "CurrentDeleteDates"] as List<DateTime>;
            }
        }
    }
}